const { login } = require('../controller/user')
const { SuccessModel, ErrorModel } = require('../model/resModel')
const { set } = require('../db/redis')
const handleUserRouter = async (req, res) => {
  const method = req.method
  // 登录
  const { username, password } = req.query

  if (method === 'GET' && req.path === '/api/user/login') {
    const result = await login(username, password)
    console.log(result)
    if (result[0]) {
      // 设置cookie
      req.session.username = result[0].username
      set(req.sessionId, req.session)
      return new SuccessModel(result[0], '登陆成功')
    } else {
      return new ErrorModel('登陆失败')
    }
  }
}

module.exports = handleUserRouter
